﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace ProblemsSet
{
    public class Problem_157 : BaseProblem
    {
        public override object GetResult()
        {
            long cnt = 0;
            for (int i = 1; i <=9; i++)
            {
                cnt += GetCountForN(i);
            }
            return cnt;
        }

        private long GetCountForN(int n)
        {
            long cnt = 0;
            //long kf2 = 1;
            //long kf5 = 1;
            List<Point> arr = new List<Point>();
            for (int st2 = 0; st2 <= n; st2++)
            {
                long A = 1;
                for (int st5 = 0; st5 <= n; st5++)
                {
                    long B = (long) Math.Pow(2, st2)*(long) Math.Pow(5, st5);
                    long sm = A + B;
                    long mlt = (n - st2 + 1)*(n - st5 + 1);
                    var lst = MathLogic.GetFactors(sm);
                    mlt *= lst.Count + 1;
                    cnt += mlt;
                    foreach (var l in lst)
                    {
                        for (var m2 = 0; m2 <= n-st2; m2++)
                        {
                            for (var m5 = 0; m5 <= n-st5; m5++)
                            {
                                long kf = (long) Math.Pow(2, m2)*(long) Math.Pow(5, m5)*l;
                                var pt = new Point((int)(kf * A), (int)(kf * B));
                                if (!arr.Contains(pt))
                                    arr.Add(pt);
                            }
                        }
                    }
                }
            }

            for (int st2 = 0; st2 <= n; st2++)
            {
                long A = (long)Math.Pow(2, st2);
                for (int st5 = 0; st5 <= n; st5++)
                {
                    long B = (long)Math.Pow(5, st5);
                    if (B < A) continue;
                    long sm = A + B;
                    long mlt = (n - st2 + 1) * (n - st5 + 1);
                    var lst = MathLogic.GetFactors(sm);
                    mlt *= lst.Count + 1;
                    cnt += mlt;
                    foreach (var l in lst)
                    {
                        for (var m2 = 0; m2 <= n - st2; m2++)
                        {
                            for (var m5 = 0; m5 <= n - st5; m5++)
                            {
                                long kf = (long)Math.Pow(2, m2) * (long)Math.Pow(5, m5) * l;
                                var pt = new Point((int) (kf*A), (int) (kf*B));
                                if (!arr.Contains(pt))
                                    arr.Add(pt);
                                //arr.Add(new Point((int)(kf * A), (int)(kf * B)));
                            }
                        }
                    }
                }
            }

            for (int st2 = 0; st2 <= n; st2++)
            {
                long B = (long)Math.Pow(2, st2);
                for (int st5 = 0; st5 <= n; st5++)
                {
                    long A = (long)Math.Pow(5, st5);
                    if (B < A) continue;
                    long sm = A + B;
                    long mlt = (n - st2 + 1) * (n - st5 + 1);
                    var lst = MathLogic.GetFactors(sm);
                    mlt *= lst.Count + 1;
                    cnt += mlt;
                    foreach (var l in lst)
                    {
                        for (var m2 = 0; m2 <= n - st2; m2++)
                        {
                            for (var m5 = 0; m5 <= n - st5; m5++)
                            {
                                long kf = (long)Math.Pow(2, m2) * (long)Math.Pow(5, m5) * l;
                                var pt = new Point((int)(kf * A), (int)(kf * B));
                                if (!arr.Contains(pt))
                                    arr.Add(pt);
                            }
                        }
                    };
                }
            }
            //return cnt;
            return arr.Count;
        }

        public override string Problem
        {
            get
            {
                return @"Consider the diophantine equation 1/a+1/b= p/10n with a, b, p, n positive integers and a  b.
For n=1 this equation has 20 solutions that are listed below:

1/1+1/1=20/10	1/1+1/2=15/10	1/1+1/5=12/10	1/1+1/10=11/10	1/2+1/2=10/10
1/2+1/5=7/10	1/2+1/10=6/10	1/3+1/6=5/10	1/3+1/15=4/10	1/4+1/4=5/10
1/4+1/20=3/10	1/5+1/5=4/10	1/5+1/10=3/10	1/6+1/30=2/10	1/10+1/10=2/10
1/11+1/110=1/10	1/12+1/60=1/10	1/14+1/35=1/10	1/15+1/30=1/10	1/20+1/20=1/10
How many solutions has this equation for 1  n  9?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 53490;
            }
        }

    }
}
